package com.zmj.framework.config;

import com.zmj.framework.authorization.filter.AuthorizationConfig;
import com.zmj.framework.authorization.filter.UserAuthRestInterceptor;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@AllArgsConstructor
@Configuration
public class WebConfig implements WebMvcConfigurer {

    private AuthorizationConfig authorizationConfig;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getUserAuthRestInterceptor()).excludePathPatterns(authorizationConfig.getIgnoreUrls());
    }

    @Bean
    UserAuthRestInterceptor getUserAuthRestInterceptor() {
        return new UserAuthRestInterceptor(authorizationConfig);
    }

    @Bean
    @ConditionalOnMissingClass(value = {"org.springframework.web.context.request.RequestContextListener"})
    public RequestContextListener requestContextListener(){
        return new RequestContextListener();
    }

}
